//
//  STSignInPrivacyView.swift
//  iPhone
//
//  Created by Toj on 4/24/23.
//

import UIKit
import YYText

class STSignInPrivacyView: UIView {
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    /* 服务 && 隐私协议提示 */
    private lazy var serviceTipsLabel: UILabel = {
        let view = UILabel()
        view.textAlignment = .center
        view.font = 12.to_systemFont
        view.textColor = "#333333".to_color
        view.text = "firstpage_tips".localized()
        return view
    }()
  
    /* 服务 && 隐私协议链接 */
    private lazy var serviceLinksLabel: YYLabel = {
        let view = YYLabel()
        view.isUserInteractionEnabled = true
        let attributedString = setupMakeServiceAttributedString()
        view.attributedText = attributedString
        return view
    }()
}

// MARK: - 点击事件
private extension STSignInPrivacyView {
    
    func didTapService() {
        print("-->toj: func:" + #function)
        let vc = ViewController()
        vc.hidesBottomBarWhenPushed = true
        vc.extendedLayoutIncludesOpaqueBars = true
        st_viewController?.navigationController?.pushViewController(vc, animated: true)
    }
    
    func didTapPrivacy() {
        print("-->toj: func:" + #function)
       
        let vc = TestViewController()
        st_viewController?.navigationController?.pushViewController(vc, animated: true)
    }
}

// MARK: - Public
extension STSignInPrivacyView {
    
}

// MARK: - Private
private extension STSignInPrivacyView {
    
}

// MARK: - 初始化设置
private extension STSignInPrivacyView {
    
    func setupMakeAddSubviews() {
        
        // 协议提示
        addSubview(serviceTipsLabel)
        // 协议链接
        addSubview(serviceLinksLabel)
    }
    
    func setupMakeLayoutSubviews() {
        
        // 协议提示
        serviceTipsLabel.snp.makeConstraints { make in
            make.left.right.equalTo(self)
            make.bottom.equalTo(snp.centerY).offset(-1.ui_mscale)
        }
        
        // 协议链接
        serviceLinksLabel.snp.makeConstraints { make in
            make.left.right.equalTo(self)
            make.top.equalTo(snp.centerY).offset(1.ui_mscale)
        }
    }
    
    func setupMakeInitialize() {
        
    }
    
    func setupMakeServiceAttributedString() -> NSAttributedString {
        let normalColor = "#333333".to_color
        let hightlightColor = "#0087EB".to_color
        let font = 12.to_systemFont

        let tipsText = "firstpage_tips2".localized()
        let text = tipsText.replacingOccurrences(of: "\"", with: "")
        let serviceLinks = tipsText.components(separatedBy: "\"")
        let serviceLink = serviceLinks.first ?? ""
        let privacyLink = serviceLinks.last ?? ""
        
        // 默认富文本
        let attributedString = text.to_attributedString(font: font, color: normalColor)
        
        let cocoaString = text as NSString
        let centerRange = cocoaString.range(of: text)
        attributedString.yy_setAlignment(.center, range: centerRange)

        // 高亮范围
        let serviceRange = cocoaString.range(of: serviceLink)
        // 添加高亮范围内属性
        let decoration = YYTextDecoration(style: .single, width: NSNumber(1), color: hightlightColor)
        attributedString.yy_setTextUnderline(decoration, range: serviceRange)
        attributedString.yy_setTextHighlight(serviceRange, color: hightlightColor, backgroundColor: nil) { [weak self] (_, _, _, _) in
            self?.didTapService()
        }
        
        // 高亮范围
        let privacyRange = cocoaString.range(of: privacyLink)
        // 添加高亮范围内属性
        attributedString.yy_setTextUnderline(decoration, range: privacyRange)
        attributedString.yy_setTextHighlight(privacyRange, color: hightlightColor, backgroundColor: nil) { [weak self] (_, _, _, _) in
            self?.didTapPrivacy()
        }
        
        return attributedString
    }
}
